﻿using System;
using System.Text;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Xml;

using XDerive.XStudio.SystemFramework;

using WebStudio.Mvc.Core.Helper;
using XDerive.XStudio.Support.SupportAppI.User;

namespace WebStudio.Mvc.Core.Extensions
{
    /// <summary>
    /// XHtmlHelper 扩展
    /// </summary>
    public static class XHtmlExtension
    {
        #region 引用插件
        /// <summary>
        /// 引用插件
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="pluginName">插件名称</param>
        /// <returns></returns>
        public static MvcHtmlString ImportPlugin(this XHtmlHelper xHelper, string pluginName)
        {
            //获取XML模板
            XmlDocument xmlDoc = new XmlDocument();
            StringBuilder builder = new StringBuilder();
            string settingPath = SysVariable.URLBase + xHelper.Url.Content("~/Scripts/plugins/Settings.xml");

            try
            {
                xmlDoc.Load(settingPath);
                XmlNode pluginConfig = xmlDoc.SelectSingleNode("Plugins");

                //获取插件节点
                XmlNode plugin = pluginConfig.SelectSingleNode("Plugin[@name='" + pluginName + "']");

                //CSS
                XmlNode pluginCss = plugin.SelectSingleNode("css");
                if (pluginCss != null)
                {
                    foreach (string css in pluginCss.InnerText.Split(','))
                    {
                        if (string.IsNullOrEmpty(css))
                        {
                            continue;
                        }
                        builder.AppendLine("<link href=\"" + xHelper.Url.Content(css) + "\" rel=\"stylesheet\" type=\"text/css\" />");
                    }
                }


                //JS
                XmlNode pluginJs = plugin.SelectSingleNode("js");
                foreach (string js in pluginJs.InnerText.Split(','))
                {
                    builder.AppendLine("<script src=\"" + xHelper.Url.Content(js) + "\" type=\"text/javascript\"></script>");
                }
            }
            catch (Exception e)
            {

            }

            return MvcHtmlString.Create(builder.ToString());
        }
        #endregion

        #region 导航链接
        /// <summary>
        /// 导航链接
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="controllerName"></param>
        /// <returns></returns>
        public static MvcHtmlString WebPartButton(this XHtmlHelper xHelper, string linkText, string actionName, string controllerName)
        {
            return WebPartButton(xHelper, linkText, actionName, controllerName, null, null, null);
        }

        /// <summary>
        /// 导航链接
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <returns></returns>
        public static MvcHtmlString WebPartButton(this XHtmlHelper xHelper, string linkText, string actionName)
        {
            return WebPartButton(xHelper, linkText, actionName, null, null, null, null);
        }

        /// <summary>
        /// 导航链接
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <returns></returns>
        public static MvcHtmlString WebPartButton(this XHtmlHelper xHelper, string linkText, string actionName, int partID)
        {
            object routeValues = null;
            object htmlAttributes = null;
            return WebPartButton(xHelper, linkText, actionName, partID, routeValues, htmlAttributes);
        }

        /// <summary>
        /// 导航链接
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="htmlAttributes"></param>
        /// <param name="routeValues"></param>
        /// <returns></returns>
        public static MvcHtmlString WebPartButton(this XHtmlHelper xHelper, string linkText, string actionName, object routeValues, object htmlAttributes)
        {
            return WebPartButton(xHelper, linkText, actionName, null, routeValues, htmlAttributes, null);
        }


        /// <summary>
        /// 导航链接
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="htmlAttributes"></param>
        /// <param name="routeValues"></param>
        /// <returns></returns>
        public static MvcHtmlString WebPartButton(this XHtmlHelper xHelper, string linkText, string actionName, int partID, object routeValues, object htmlAttributes)
        {
            string controllerName = String.Empty;
            return WebPartButton(xHelper, linkText, actionName, controllerName, partID, routeValues, htmlAttributes);
        }
        /// <summary>
        /// 导航链接
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="htmlAttributes"></param>
        /// <param name="routeValues"></param>
        /// <returns></returns>
        public static MvcHtmlString WebPartButton(this XHtmlHelper xHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes)
        {
            return WebPartButton(xHelper, linkText, actionName, controllerName, routeValues, htmlAttributes, null);
        }

        /// <summary>
        /// 导航链接
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="htmlAttributes"></param>
        /// <param name="routeValues"></param>
        /// <returns></returns>
        public static MvcHtmlString WebPartButton(this XHtmlHelper xHelper, string linkText, string actionName, string controllerName, int partID, object routeValues, object htmlAttributes)
        {
            string activedClassName = String.Empty;
            return WebPartButton(xHelper, linkText, actionName, controllerName, partID, routeValues, htmlAttributes, activedClassName);
        }
        /// <summary>
        /// 导航链接
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="htmlAttributes"></param>
        /// <param name="routeValues"></param>
        /// <param name="activedClassName">激活样式</param>
        /// <returns></returns>
        public static MvcHtmlString WebPartButton(this XHtmlHelper xHelper, string linkText, string actionName, string controllerName, object routeValues, object htmlAttributes, string activedClassName)
        {
            int partID = 0;
            return WebPartButton(xHelper, linkText, actionName, controllerName, partID, routeValues, htmlAttributes, activedClassName);
        }
        /// <summary>
        /// 导航链接
        /// </summary>
        /// <param name="xHelper"></param>
        /// <param name="linkText"></param>
        /// <param name="actionName"></param>
        /// <param name="htmlAttributes"></param>
        /// <param name="routeValues"></param>
        /// <param name="activedClassName">激活样式</param>
        /// <returns></returns>
        public static MvcHtmlString WebPartButton(this XHtmlHelper xHelper, string linkText, string actionName, string controllerName, int partID, object routeValues, object htmlAttributes, string activedClassName)
        {
            if (partID > 0)
            {
                if (!UserAppI.CheckPower(partID))
                {
                    return MvcHtmlString.Create(string.Empty);
                }
            }
            StringBuilder builder = new StringBuilder();
            bool isActived = true;
            if (string.IsNullOrEmpty(activedClassName))
            {
                activedClassName = "btn-highlight";
            }

            //判断当前Action 是否与连接Action 相同
            isActived = isActived && string.Equals(xHelper.Html.ViewContext.RouteData.Values["Action"].ToString(), actionName, StringComparison.InvariantCultureIgnoreCase);

            //判断Controller是否相同
            if (!string.IsNullOrEmpty(controllerName))
            {
                isActived = isActived && string.Equals(xHelper.Html.ViewContext.RouteData.Values["Controller"].ToString(), controllerName, StringComparison.InvariantCultureIgnoreCase);
            }

            //与当前Action 不同时，判断ViewData中的CurrentAction是否同链接Action相同
            if (!isActived)
            {
                //HTML属性设置  
                string currAction = SysFunction.StringParse(xHelper.Html.ViewData["CurrentAction"]);
                string currController = SysFunction.StringParse(xHelper.Html.ViewData["CurrentController"]);
                if (!string.IsNullOrEmpty(currAction))
                {
                    isActived = string.Equals(actionName, currAction, StringComparison.InvariantCultureIgnoreCase);
                }
                if (!string.IsNullOrEmpty(currController))
                {
                    isActived = isActived && string.Equals(currController, controllerName, StringComparison.InvariantCultureIgnoreCase);
                }
            }

            builder.AppendFormat("<li class='{0}'>", isActived ? activedClassName : "");
            if (string.IsNullOrEmpty(controllerName))
            {
                builder.Append(xHelper.Html.ActionLink(linkText, actionName, routeValues, htmlAttributes));
            }
            else
            {
                builder.Append(xHelper.Html.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes));
            }
            builder.Append("</li>");

            return MvcHtmlString.Create(builder.ToString());
        }
        #endregion

    }
}
